home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue34 / real3d / _setup.1 / Unit1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-03-26  |  10.2 KB  |  398 lines

  1. { ========================================================= }
  2.  
  3. unit Unit1;
  4.  
  5. interface
  6.  
  7. uses
  8.    Windows,
  9.    Messages,
  10.    SysUtils,
  11.    Classes,
  12.    Graphics,
  13.    Controls,
  14.    Forms,
  15.    Dialogs,
  16.    ExtCtrls,
  17.    View3D,
  18.    Mat3D,
  19.    Model3D,
  20.    Math3D,
  21.    Ref3D,
  22.    Route3D,
  23.    Text3D;
  24.  
  25. { ========================================================= }
  26.  
  27. const
  28.    CylinderRadius : Single   = 10.0;
  29.    CylinderHeight : Single   = 20.0;
  30.  
  31. { ========================================================= }
  32.  
  33. type
  34.    TForm1 = class(TForm)
  35.       View3D1 : TView3D;
  36.       Timer1  : TTimer;
  37.       procedure FormCreate(Sender: TObject);
  38.       procedure FormMouseMove(Sender: TObject;
  39.                               Shift: TShiftState; X, Y: Integer);
  40.       procedure FormDestroy(Sender: TObject);
  41.       procedure FormResize(Sender: TObject);
  42.       procedure FormPaint(Sender: TObject);
  43.       procedure Timer1Timer(Sender: TObject);
  44.  
  45.       private
  46.          procedure ConstructCylinder;
  47.          procedure ConstructRoute;
  48.          procedure Draw;
  49.          procedure LoadTextures;
  50.  
  51.       public
  52.          child1   : TView3DReference;
  53.          child2   : TView3DReference;
  54.          material : array [0..11] of TView3DMaterial;
  55.          model    : TView3DModel;
  56.          parent   : TView3DReference;
  57.          radius   : Single;
  58.          texture  : array[0..5] of TView3DTexture;
  59.          route    : TView3DRoute;
  60.          time     : Single;
  61.    end;
  62.  
  63. { ========================================================= }
  64.  
  65. var
  66.    Form1 : TForm1;
  67.  
  68. { ========================================================= }
  69.  
  70. implementation
  71.  
  72. {$R *.DFM}
  73.  
  74. { ======================================================== }
  75. { === Procedure to draw the view                       === }
  76. { ======================================================== }
  77. procedure TForm1.Draw;
  78.  
  79. begin
  80.    View3D1.Draw;
  81. end;
  82.  
  83. { ======================================================== }
  84. { === Procedure to construct the view, called when the === }
  85. { === form is created                                  === }
  86. { ======================================================== }
  87. procedure TForm1.FormCreate(Sender: TObject);
  88.  
  89. begin
  90.    { === create a model === }
  91.    model := TView3DModel.Create;
  92.  
  93.    { === create some references === }
  94.    parent := TView3DReference.Create;
  95.    child1 := TView3DReference.Create;
  96.    child2 := TView3DReference.Create;
  97.  
  98.    { === set the child references to reference the model === }
  99.    child1.Model := @model;
  100.    child2.Model := @model;
  101.  
  102.    { === offset the position of the two child references === }
  103.    child1.Position.X := CylinderRadius;
  104.    child2.Position.X := -CylinderRadius;
  105.  
  106.    { === add the child references to the parent === }
  107.    parent.Add(@child1);
  108.    parent.Add(@child2);
  109.  
  110.    { === load textures === }
  111.    LoadTextures();
  112.  
  113.    { === build a cylinder === }
  114.    ConstructCylinder();
  115.  
  116.     { === calculate polygon vertex normals === }
  117.    model.Smooth(-180.0, 0.0001);
  118.  
  119.    { === add reference to view === }
  120.    View3D1.AddReference(@parent);
  121.  
  122.    { === initialise eye radius === }
  123.    radius := Sqrt(View3D1.Eye.X * View3D1.Eye.X +
  124.                   View3D1.Eye.Y * View3D1.Eye.Y +
  125.                   View3D1.Eye.Z * View3D1.Eye.Z);
  126.  
  127.    { === construct route === }
  128.    ConstructRoute();
  129.  
  130. end;
  131.  
  132. { ======================================================== }
  133. { === Procedure to load textures                       === }
  134. { ======================================================== }
  135. procedure TForm1.LoadTextures();
  136.  
  137. var
  138.    bitmap : TBitmap;
  139.    i      : Integer;
  140.    name   : TFileName;
  141.  
  142. begin
  143.    { === initialise texture bitmap file name === }
  144.    name := 'textureX.bmp';
  145.  
  146.    { === create a bitmap === }
  147.    bitmap := TBitmap.Create;
  148.  
  149.    { === load six textures === }
  150.    for i := 0 to 5 do begin
  151.       name[8] := Char(i + Integer('1'));
  152.  
  153.       { === create a texture === }
  154.       texture[i] := TView3DTexture.Create;
  155.  
  156.       { === load bitmap from file === }
  157.       bitmap.LoadFromFile(name);
  158.  
  159.       { === assign bitmap to texture === }
  160.       texture[i].Bitmap  := bitmap;
  161.  
  162.       { === set texture quality and combination mode === }
  163.       texture[i].Quality := Low;
  164.       texture[i].Combine := Modulate;
  165.    end;
  166. end;
  167.  
  168. { ======================================================== }
  169. { === Procedure to construct a 12-sided cylinder       === }
  170. { ======================================================== }
  171. procedure TForm1.ConstructCylinder();
  172.  
  173. var
  174.    angle  : Single;
  175.    i      : Integer;
  176.    pt     : array [0..23] of TView3DPoint3;
  177.    vertex : array [0..3] of TView3DVertex;
  178.  
  179. begin
  180.    { === setup points for === }
  181.    for i := 0 to 11 do begin
  182.       angle := i * 2.0 * Pi / 12;
  183.       pt[i].x := CylinderRadius * Sin(angle);
  184.       pt[i].y := -CylinderHeight;
  185.       pt[i].z := CylinderRadius * Cos(angle);
  186.       pt[i + 12].x := pt[i].x;
  187.       pt[i + 12].y := CylinderHeight;
  188.       pt[i + 12].z := pt[i].z;
  189.    end;
  190.  
  191.    { === setup polygons === }
  192.    for i := 0 to 11 do begin
  193.  
  194.       { === create a material === }
  195.       material[i] := TView3DMaterial.Create;
  196.  
  197.       { === set material color and texture === }
  198.       material[i].Diffuse.Red   := 255;
  199.       material[i].Diffuse.Green := 255;
  200.       material[i].Diffuse.Blue  := 255;
  201.       material[i].texture := @texture[i mod 6];
  202.  
  203.       { === setup vertices === }
  204.       vertex[0].point := pt[i];
  205.       vertex[0].texture.x := 0.0;
  206.       vertex[0].texture.y := 0.0;
  207.  
  208.       vertex[1].point := pt[(i + 1) mod 12];
  209.       vertex[1].texture.x := 1.0;
  210.       vertex[1].texture.y := 0.0;
  211.  
  212.       vertex[2].point := pt[(i + 1) mod 12 + 12];
  213.       vertex[2].texture.x := 1.0;
  214.       vertex[2].texture.y := 1.0;
  215.  
  216.       vertex[3].point := pt[i + 12];
  217.       vertex[3].texture.x := 0.0;
  218.       vertex[3].texture.y := 1.0;
  219.  
  220.       { === add polygons (two triangles) to model === }
  221.       model.AddPolygon(vertex[0], vertex[1], vertex[2], @material[i]);
  222.       model.AddPolygon(vertex[0], vertex[2], vertex[3], @material[i]);
  223.    end;
  224. end;
  225.  
  226. { ======================================================== }
  227. { === Procedure to construct a simple route            === }
  228. { ======================================================== }
  229. procedure TForm1.ConstructRoute();
  230.  
  231. var
  232.    w : array[0..3] of TView3DRouteWayPt;
  233.  
  234. begin
  235.    { === create route object === }
  236.    route := TView3DRoute.Create;
  237.  
  238.    { === initialise way points === }
  239.    w[0].x := -50.0;
  240.    w[0].y := 0.0;
  241.    w[0].z := 0.0;
  242.    w[0].pitch := 0.0;
  243.    w[0].yaw   := 0.0;
  244.    w[0].roll  := 0.0;
  245.    w[0].speed := 25.0;
  246.  
  247.    w[1].x := 0.0;
  248.    w[1].y := 200.0;
  249.    w[1].z := radius;
  250.    w[1].pitch := 0.0;
  251.    w[1].yaw   := 90.0;
  252.    w[1].roll  := 0.0;
  253.    w[1].speed := 25.0;
  254.  
  255.    w[2].x := 50.0;
  256.    w[2].y := 0.0;
  257.    w[2].z := 0.0;
  258.    w[2].pitch := 0.0;
  259.    w[2].yaw   := 180.0;
  260.    w[2].roll  := 0.0;
  261.    w[2].speed := 25.0;
  262.  
  263.    w[3].x := 0.0;
  264.    w[3].y := 0.0;
  265.    w[3].z := -radius * 1.5;
  266.    w[3].pitch := 0.0;
  267.    w[3].yaw   := 270.0;
  268.    w[3].roll  := 0.0;
  269.    w[3].speed := 25.0;
  270.  
  271.    { === add way points === }
  272.    route.Add(w[0]);
  273.    route.Add(w[1]);
  274.    route.Add(w[2]);
  275.    route.Add(w[3]);
  276.  
  277.    { === set route to auto-loop === }
  278.    route.continuous := True;
  279.  
  280.    { === initialise time === }
  281.    time := 0.0;
  282. end;
  283.  
  284. { ======================================================== }
  285. { === Procedure to change the eye point depending on   === }
  286. { === the mouse position                               === }
  287. { ======================================================== }
  288. procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  289.  
  290. var
  291.    lat  : Single;
  292.    long : Single;
  293.  
  294. begin
  295.  
  296.    { === calculate yaw value === }
  297.    View3D1.Eye.Yaw := (X - ClientWidth / 2) / ClientWidth * 360.0;
  298.  
  299.    { === calculate pitch value === }
  300.    View3D1.Eye.Pitch := (Y - ClientHeight / 2) / ClientHeight * 180.0;
  301.  
  302.    { === calculate x, y and z values === }
  303.    long := DegToRad(View3D1.Eye.Yaw) + Pi;
  304.    lat := DegToRad(View3D1.Eye.Pitch);
  305.    View3D1.Eye.X := radius * Sin(long) * Cos(lat);
  306.    View3D1.Eye.Y := radius * Sin(lat);
  307.    View3D1.Eye.Z := radius * Cos(long) * Cos(lat);
  308. end;
  309.  
  310. { ======================================================== }
  311. { === Procedure to destroy objects                     === }
  312. { ======================================================== }
  313. procedure TForm1.FormDestroy(Sender: TObject);
  314.  
  315. var
  316.     i : Integer;
  317.  
  318. begin
  319.    { === empty model === }
  320.    model.Empty;
  321.  
  322.    { === destroy model === }
  323.    model.Destroy;
  324.  
  325.    { === destroy references === }
  326.    parent.Destroy;
  327.    child1.Destroy;
  328.    child2.Destroy;
  329.  
  330.    { === destroy textures === }
  331.    for i := 0 to 5 do begin
  332.       texture[i].Destroy;
  333.     end;
  334.  
  335.    { === destroy materials === }
  336.    for i := 0 to 11 do begin
  337.       material[i].Destroy;
  338.    end;
  339.  
  340.    { === destroy route === }
  341.    route.Destroy;
  342. end;
  343.  
  344. { ======================================================== }
  345. { === Procedure to redraw the view when the form is    === }
  346. { === resized                                          === }
  347. { ======================================================== }
  348. procedure TForm1.FormResize(Sender: TObject);
  349.  
  350. begin
  351.    { === set new width & height === }
  352.    View3D1.Size.Width  := ClientWidth;
  353.    View3D1.Size.Height := ClientHeight;
  354.  
  355.    { === draw === }
  356.    Draw;
  357. end;
  358.  
  359. { ======================================================== }
  360. { === Procedure to redraw the view when the form is    === }
  361. { === repainted                                        === }
  362. { ======================================================== }
  363. procedure TForm1.FormPaint(Sender: TObject);
  364.  
  365. begin
  366.    { === draw === }
  367.    Draw;
  368. end;
  369.  
  370. { ======================================================== }
  371. { === Procedure to handle timer for animation          === }
  372. { ======================================================== }
  373. procedure TForm1.Timer1Timer(Sender: TObject);
  374.  
  375. begin
  376.    { === rotate references === }
  377.    child1.Position.Pitch := child1.Position.Pitch + 10.0;
  378.    child2.Position.Yaw   := child2.Position.Yaw - 10.0;
  379.  
  380.    { === evaluate route === }
  381.    route.Evaluate(time);
  382.  
  383.    { === inrement time === }
  384.    time := time + 0.1;
  385.  
  386.    { === set parent position === }
  387.    parent.Position := route.Position;
  388.  
  389.    { === draw === }
  390.    Draw();
  391. end;
  392.  
  393. end.
  394.  
  395. { ========================================================= }
  396.  
  397.  
  398.